package com.dragonwu.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 *
 * @author DragonWu
 * @since 2022-10-07 20:19
 **/

@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /*
    设置对象
     */
    public void setObject(String key, Object object, long time, TimeUnit timeUnit) {
        try {
            redisTemplate.opsForValue().set(key, object, time, timeUnit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    获取对象
     */
    public Object getObject(String key) {
        try {
            return redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    删除对象
     */
    public void deleteObject(String key) {
        try {
            redisTemplate.delete(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
